using System.Collections.Generic;
using UnityEngine;

public class UI : MonoBehaviour
{
    private PlayerInput input;
    public UI_SkillToolTip skillToolTip { get; private set; }
    public UI_ItemToolTip itemToolTip { get; private set; }
    public UI_StatToolTip statToolTip { get; private set; }

    public UI_SkillTree skillTreeUI { get; private set; }
    public UI_Inventory inventoryUI { get; private set; }
    public UI_Storage storageUI { get; private set; }
    public UI_Craft craftUI { get; private set; }
    public UI_Merchant merchantUI { get; private set; }
    public UI_InGame inGameUI { get; private set; }
    public UI_Option opitionUI { get; private set; }
    public UI_GameOver gameOverUI { get; private set; }
    public UI_Quest questUI { get; private set; }
    public UI_FadeEffect fadeEffect { get; private set; }
    public UI_Dialogue dialogueUI { get; private set; }
    [SerializeField] private List<GameObject> panelUIList;
    private bool inventoryUIToggle;
    private void Awake()
    {
        skillToolTip = GetComponentInChildren<UI_SkillToolTip>();
        itemToolTip = GetComponentInChildren<UI_ItemToolTip>();
        statToolTip = GetComponentInChildren<UI_StatToolTip>();

        skillTreeUI = GetComponentInChildren<UI_SkillTree>(true);
        inventoryUI = GetComponentInChildren<UI_Inventory>(true);
        storageUI = GetComponentInChildren<UI_Storage>(true);
        craftUI = GetComponentInChildren<UI_Craft>(true);
        merchantUI = GetComponentInChildren<UI_Merchant>(true);
        opitionUI = GetComponentInChildren<UI_Option>(true);
        gameOverUI = GetComponentInChildren<UI_GameOver>(true);
        fadeEffect = GetComponentInChildren<UI_FadeEffect>();
        inGameUI = GetComponentInChildren<UI_InGame>();
        questUI = GetComponentInChildren<UI_Quest>(true);
        dialogueUI = GetComponentInChildren<UI_Dialogue>(true);
        
        inventoryUIToggle = inventoryUI.gameObject.activeSelf;
    }
    public void SetUI(PlayerInput input)
    {
        this.input = input;
        skillTreeUI.UpdateAllDefaultSkillUnlock();

        input.UI.InventoryUI.performed += context => OpenCharacterUI();
        input.UI.OptionUI.performed += context =>
        {
            foreach (var panel in panelUIList)
            {
                if (panel.activeSelf)
                {
                    SwitchPanelToInGame();
                    Time.timeScale = 1f;
                    return;
                }
            }
            OpenOpitionUI();
            Time.timeScale = 0f;
        };

        input.UI.DialogueInteraction.performed += context =>
        {
            if (dialogueUI.gameObject.activeSelf)
                dialogueUI.DialogueInteraction();
        };

        input.UI.DialogueNavigation.performed += context =>
        {
            int direction = Mathf.FloorToInt(context.ReadValue<float>());
            if (dialogueUI.gameObject.activeSelf)
                dialogueUI.NaivgationChoice(direction);
        };
    }

    private void OpenOpitionUI()
    {
        SwitchTo(opitionUI.gameObject);
    }

    public void OpenGameOverUI()
    {
        SwitchTo(gameOverUI.gameObject);
    }
    
    //打开玩家面板
    public void OpenCharacterUI()
    {
        //如果玩家面板未打开
        if (inventoryUIToggle == false)
        {
            //将面板标记启用
            inventoryUIToggle = true;
            inventoryUI.gameObject.SetActive(true);
            //停止玩家输入
            StopPlayerControl();
            return;
        }

        //如果玩家面板打开并且当前打开的面板是玩家技能面板
        if (skillTreeUI.gameObject.activeSelf)
        {
            inventoryUI.gameObject.SetActive(true);
            skillTreeUI.gameObject.SetActive(false);
            return;
        }

        //如果玩家面板打开并且当前打开的面板是玩家属性面板
        inventoryUI.gameObject.SetActive(false);
        StopPlayerControl();
        //将面板标记未启用
        inventoryUIToggle = false;
    }

    //打开商店面板
    public void OpenMerchantUI(bool toggle)
    {
        merchantUI.gameObject.SetActive(toggle);
        StopPlayerControl();
        StopKeyBoardUIInput(toggle);
    }

    //打开锻造面板
    public void OpenCraftAndStorageUI(bool toggle)
    {
        craftUI.gameObject.SetActive(toggle);
        StopPlayerControl();
        StopKeyBoardUIInput(toggle);
        if (toggle == false)
            storageUI.gameObject.SetActive(toggle);
    }

    public void OpenQuestUI(QuestDataSO[] questsToShow)
    {
        questUI.gameObject.SetActive(true);
        StopPlayerControl();
        questUI.SetupQuestUI(questsToShow);
    }
    public void OpenDialogueUI(DialogueLineSO line,DialogueNPCData dialogueNPCData)
    {
        dialogueUI.gameObject.SetActive(true);

        StopPlayerControl();
        dialogueUI.SetupDialogueUI(dialogueNPCData);
        dialogueUI.PlayerDialogueLine(line);

    }
    //停止按键打开面板操作
    public void StopKeyBoardUIInput(bool toggle)
    {
        if (toggle)
            input.UI.InventoryUI.Disable();
        else
            input.UI.InventoryUI.Enable();
    }
    private void SwitchTo(GameObject panelUI)
    {
        foreach (var panel in panelUIList)
            panel.SetActive(false);

        panelUI.gameObject.SetActive(true);
        StopPlayerControl();
    }


    //停止玩家输入
    public void StopPlayerControl()
    {
        HideAllToolTip();

        foreach (var panel in panelUIList)
        {
            if (panel.activeSelf)
            {
                input.Player.Disable();
                return;
            }
        }
        input.Player.Enable();
    }

    //如果存在已打开的面板，关闭面板
    public void SwitchPanelToInGame()
    {
        foreach (var panel in panelUIList)
            if (panel.activeSelf)
                panel.SetActive(false);

        StopPlayerControl();
        StopKeyBoardUIInput(false);
        inGameUI.gameObject.SetActive(true);
        inventoryUIToggle = false;

        inGameUI.questTip.SetupQuestTip(Player.Instance.questManager.GetQuestList());
    }

    //隐藏所有提示板
    public void HideAllToolTip()
    {
        skillToolTip.ShowToolTip(false, null);
        itemToolTip.ShowToolTip(false, null);
        statToolTip.ShowToolTip(false, null);
    }
}
